﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace CRM.Cache
{
    public class DictinaryCache : ICache
    {
        //10s超时
        readonly static int timeout = 1000 * 10;

        static ReaderWriterLockSlim rwl = new ReaderWriterLockSlim();

        static Dictionary<string, object> dictionary = new Dictionary<string, object>();

        /// <summary>
        /// 该字典不实现此方法
        /// </summary>
        /// <param name="argKey"></param>
        /// <param name="argValue"></param>
        /// <param name="argDateExpiration"></param>
        /// <returns></returns>
        public bool ContainKey(string argKey)
        {
            try
            {
                rwl.EnterReadLock();

                return dictionary.ContainsKey(argKey);

            }
            finally
            {
                rwl.ExitReadLock();
            }
        }

        public bool Add(string argKey, object argValue)
        {
            try
            {
                rwl.EnterWriteLock();

                if (!dictionary.ContainsKey(argKey))
                {
                    dictionary[argKey] = argValue;
                }
                return true;
            }
            finally
            {
                rwl.ExitWriteLock();
            }
        }

        /// <summary>
        /// 该字典不实现此方法
        /// </summary>
        /// <param name="argKey"></param>
        /// <param name="argValue"></param>
        /// <param name="argDateExpiration"></param>
        /// <returns></returns>
        public bool Add(string argKey, object argValue, DateTime argDateExpiration)
        {
            return false;
        }

        public bool Set(string argKey, object argValue)
        {
            try
            {
                rwl.EnterWriteLock();

                if (!dictionary.ContainsKey(argKey))
                {
                    dictionary[argKey] = argValue;
                }
                return true;
            }
            finally
            {
                rwl.ExitWriteLock();
            }
        }


        /// <summary>
        /// 该字典不实现此方法
        /// </summary>
        /// <param name="argKey"></param>
        /// <param name="argValue"></param>
        /// <param name="argDateExpiration"></param>
        /// <returns></returns>
        public bool Set(string argKey, object argValue, DateTime argDateExpiration)
        {
            throw new NotImplementedException();
        }

        public object Get(string argKey)
        {
            try
            {
                rwl.EnterReadLock();

                if (!dictionary.ContainsKey(argKey))
                {
                    return null;
                }
                return dictionary[argKey];
            }
            finally
            {
                rwl.ExitReadLock();
            }
        }

        public bool Remove(string argKey)
        {
            try
            {
                rwl.EnterWriteLock();

                //遍历次数O(n)
                for (int i = 0; i < dictionary.Keys.Count; i++)
                {
                    //获取当前集合元素
                    var item = dictionary.Keys.ElementAt(i);

                    if (item != null && item.Contains(argKey))
                    {
                        dictionary.Remove(item);
                        i--;
                    }

                }
                return true;
            }
            finally
            {
                rwl.ExitWriteLock();
            }
        }

        /// <summary>
        /// 该字典不实现此方法
        /// </summary>
        /// <param name="argKey"></param>
        /// <param name="argValue"></param>
        /// <param name="argDateExpiration"></param>
        /// <returns></returns>
        public bool Remove(string argKey, DateTime argDateExpiration)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 该字典不实现此方法
        /// </summary>
        /// <param name="argKey"></param>
        /// <param name="argValue"></param>
        /// <param name="argDateExpiration"></param>
        /// <returns></returns>
        public bool Remove()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 该字典不实现此方法
        /// </summary>
        /// <param name="argKey"></param>
        /// <param name="argValue"></param>
        /// <param name="argDateExpiration"></param>
        /// <returns></returns>
        public bool Remove(System.Collections.ArrayList servers)
        {
            throw new NotImplementedException();
        }
    }
}
